package com.hilaver.dzmis.service.impl;

import java.util.List;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryButton;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.codetable.CtYarnType;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.service.IEntityService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class BiYarnServiceImpl extends AbstractBaseService implements
		IEntityService {

	public static final String COLUMN_YARN_TYPE = "ctYarnType";
	
	private CtServiceImpl ctService;
	
	public BiYarnServiceImpl(){
		this.ctService = new CtServiceImpl();
	}
	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(BiYarn.class.getName(), id);
		return super.delete(BiYarn.class.getName(), id);
	}
	
	public String edit(Object object) throws Exception {
		BiYarn biYarn = (BiYarn)object;
		String operation = null;
		if (biYarn.getId() != null && biYarn.getPhotoName() == null) {
			BiYarn old = (BiYarn) this.dao.get(
					BiYarn.class.getName(), biYarn.getId());
			biYarn.setPhoto(old.getPhoto());
			biYarn.setPhotoName(old.getPhotoName());
			this.dao.getSession().evict(old);
			operation = "02";
		}else{
			operation = "01";
		}
		this.dao.saveOrUpdate(biYarn);
		super.editSysMbox(biYarn, operation);
		return "{success: true}";
	}

	@Override
	public String get(int id) throws Exception {
		BiYarn obj = (BiYarn)this.dao.get(BiYarn.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		if(obj.getBiFactory() != null){
			xmlSB.append("<" + COLUMN_FACTORY_ID + ">");
			xmlSB.append(obj.getBiFactory().getId());
			xmlSB.append("</" + COLUMN_FACTORY_ID + ">");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFactory().getNameEN()==null?"":obj.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		xmlSB.append("<" + COLUMN_PHOTO_NAME + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (obj.getPhotoName() == null || "".equals(obj.getPhotoName())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + obj.getPhotoName(), obj.getPhoto());
			xmlSB.append(obj.getPhotoName());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_PHOTO_NAME + ">");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		return super.getAllPagination(BiYarn.class.getName(), page, sort, order);
	}
	
	public String getAllPagination(int page, String sort, String order, String[] filters)
			throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<BiYarn> objList = this.dao.getAllPagination(BiYarn.class.getName(), offset,
				Constants.PAGE_SIZE, sort, order, filters);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (BiYarn obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_YARN_TYPE + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnType.class.getName(), obj.getCtYarnType(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_YARN_TYPE + ">");
			
			xmlSB.append("<ctYarnMeasure>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMeasure.class.getName(), obj.getCtYarnMeasure(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMeasure>");
			
			xmlSB.append("<yarnAccount>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(obj.getYarnAccount());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</yarnAccount>");
			
			xmlSB.append("<ctYarnMaterial1>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial1(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial1()) && obj.getComposition1() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition1() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial1>");
			
			xmlSB.append("<ctYarnMaterial2>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial2(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial2()) && obj.getComposition2() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition2() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial2>");
			
			xmlSB.append("<ctYarnMaterial3>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial3(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial3()) && obj.getComposition3() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition3() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial3>");
			
			xmlSB.append("<ctYarnMaterial4>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial4(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial4()) && obj.getComposition4() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition4() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial4>");
			
			xmlSB.append("<ctYarnMaterial5>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtYarnMaterial.class.getName(), obj.getCtYarnMaterial5(), locale));
			if(!StringUtils.isEmpty(obj.getCtYarnMaterial5()) && obj.getComposition5() != null){
				xmlSB.append("&nbsp;(&nbsp;" + obj.getComposition5() + "%&nbsp;)");
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctYarnMaterial5>");
			
			
			if(obj.getBiFactory() != null){
				xmlSB.append("<factoryNameCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameCN>");
				
				xmlSB.append("<factoryNameEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getNameEN()==null?"":obj.getBiFactory().getNameEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</factoryNameEN>");
				
				xmlSB.append("<addressFactoryCN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryCN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryCN>");
				
				xmlSB.append("<addressFactoryEN>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(obj.getBiFactory().getAddressFactoryEN());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</addressFactoryEN>");
			}
			
			xmlSB.append("<" + COLUMN_PHOTO_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (obj.getPhotoName() == null || "".equals(obj.getPhotoName())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + obj.getPhotoName(), obj.getPhoto());
				xmlSB.append(obj.getPhotoName());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_PHOTO_NAME + ">");
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(BiYarn.class.getName(), filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAll(String sort, String order) throws Exception{
		return super.getAll(BiYarn.class.getName(), sort, order);
	}
	
	public BiYarn getObj(int id) throws Exception {
		return (BiYarn)super.getObj(BiYarn.class.getName(), id);
	}

}
